package JackSu.简单;

public class _03罗马数字转整数 {
    public static void main(String[] args) {

        String h="MCMXCIV";
        int i = romanToInt(h);
        System.out.println(i);


    }


    public static int romanToInt(String s) {
        int rs=0;

        char[] chars = s.toCharArray();
        //先和这个数组里的值比较 获得各个罗马数字的下标
        char[] a = {'I', 'V', 'X', 'L', 'C', 'D', 'M'};
        int[] c = new int[chars.length];
        int index = 0;
        //再把下标对应的数字拿出来
        int[] b = {1, 5, 10, 50, 100, 500, 1000};
        for (int i = 0; i <= chars.length - 1; i++) {
            for (int j = 0; j <= a.length - 1; j++) {
                if (a[j] == chars[i]) {
                    c[index] = j;
                    index++;
                }

            }
        }
        int sum=0;
        //对这个数组排序，让他从大到小排，如果移动了一次就-1
        for (int i = 0; i <c.length; i++) {
            if (i<c.length-1&&b[c[i]]<b[c[i+1]]) {
                sum-=b[c[i]];
            }else {
                sum+=b[c[i]];
            }

            }


return  sum;

    }
}
